﻿using Survey.DataAccessLayer;
using Survey.DataAccessLayer.Data;
using Survey.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebUI.Business
{
    public class QuestionnaireBusiness
    {
        


        #region 问卷管理
        private QuestionnaireAccessor questionnaireAccessor = new QuestionnaireAccessor();
        //api/EditQuestionnaireById
        public Object EditQuestionnaireById(HttpSessionStateBase session,int id)
        {
            //身份验证
            var curentUserId = session["curentUserId"];
            //没有登陆
            if (curentUserId == null)
            {
                return new Questionnaire
                {
                    Id = -1,
                    QuestionCollection = null,
                    Describe = "你没有登陆，不能修改",
                    Title = "你没有登陆，不能修改",
                };
            }
            

            try
            {
                //有这个问卷，且问卷属于操作者
                var q = questionnaireAccessor.Select(qq => qq.Id == id && qq.UserId == (int)curentUserId).Single();
                return Units.DataProcess.Obj2Json(q);
            }
            catch (Exception)
            {

                return new Questionnaire
                {
                    Id = -1,
                    QuestionCollection = null,
                    Describe = "没有找到这个问卷，或者你问卷不属于你，不能修改",

                    Title = "没有找到这个问卷，或者你问卷不属于你，不能修改",
                };

            }

        }
        //api/CreateQuestionnaire
        public Object CreateQuestionnaire(HttpSessionStateBase session,Questionnaire questionnaire)
        {
            var curentUserId = session["curentUserId"];
            if (questionnaire == null)
            {
                return new { Result = false, Msg = "无效的问卷" };
            }
            if (curentUserId == null)
            {
                return new { Result = false, Msg = "请登陆" };
            }
            try
            {

                questionnaire.UserId = (int)curentUserId;
                questionnaireAccessor.Add(questionnaire);
                bool isOk = questionnaireAccessor.Save() > 0;
                return new { Result = isOk, Msg = "添加成功" };
            }
            catch (Exception e)
            {
                return new { Result = false, Msg = e.Message };
            }
        }
        //api/DeleteQuestionnaireById
        public Object DeleteQuestionnaireById(HttpSessionStateBase session, int id)
        {
            var curentUserId = session["curentUserId"];
            if (curentUserId == null)
            {
                return new { Result = false, Msg = "请登陆" };
            }
            if (id <= 0)
                return new { Result = false, Msg = "Id无效" };
            try
            {
                //有这个问卷，且问卷属于自己
                var q = questionnaireAccessor.Select(qq=>qq.Id==id&&qq.UserId ==(int)curentUserId).Single();

                questionnaireAccessor.Delete(q);

                questionnaireAccessor.Save();
                return new { Result = true,Msg = "成功" };
            }
            catch (Exception e)
            {
                return new { Result = false, Msg = e.Message };
            }
        }
        //api/PostEditedQuestionnaire
        public Object PostEditedQuestionnaire(HttpSessionStateBase session
            ,Questionnaire q)
        {
            var curentUserId = session["curentUserId"];

            if (q.Id <= 0)
                return new { Result = false, Msg = "Id不能为空" };

            try
            {
                //存在问卷，且属于自己
                bool isOk = questionnaireAccessor.Select(qq => qq.Id == q.Id && qq.UserId == (int)curentUserId).Count()>0;
                if (!isOk)
                {
                    return new { Result = false, Msg = "问卷不存在或者不属于自己" };
                }

                questionnaireAccessor.Update(q);
                questionnaireAccessor.Save();
                return new { Result = true, Msg = "成功" };
            }
            catch (Exception e)
            {
                return new { Result = false, Msg = e.Message };
            }

        }
        #endregion


    }
}